using UnityEngine;

//生成区
public abstract class SpawnZone : PersistableObject
{
    //获取生成点
    public abstract Vector3 SpawnPoint{get;}

    
    public virtual Shape SpawnShape()
    {
        int factoryIndex = Random.Range(0, spawnConfig.factories.Length);
        Shape shape = spawnConfig.factories[factoryIndex].GetRandom();

        Transform t = shape.transform;
        t.localPosition = SpawnPoint;
        t.localRotation = Random.rotation;//随机旋转
        t.localScale = Vector3.one * spawnConfig.scale.RandomValueInRange;//随机比例
        if(spawnConfig.uniformColor)
            shape.SetColor(spawnConfig.color.RandomInRange);
        else{
            for(int i = 0;i<shape.ColorCount;i++)
            {
                shape.SetColor(spawnConfig.color.RandomInRange, i);//随机颜色
            }
        }
        float angularSpeed = spawnConfig.angularSpeed.RandomValueInRange;
        if(angularSpeed != 0)
        {
            //将行为组件天骄到形状中并设置属性
            var rotation = shape.AddBehavior<RotationShapeBehavior>();
            rotation.AngularVelocity = Random.onUnitSphere * angularSpeed;//设置随机旋转轴

        }
        float speed = spawnConfig.Speed.RandomValueInRange;
        if(speed != 0f)
        {
            Vector3 direction;

            switch(spawnConfig.moveMentDirection)
            {
                case SpawnConfiguration.MoveMentDirection.Upward:
                    direction = transform.up;
                    break;
                case SpawnConfiguration.MoveMentDirection.Outward:
                    //向外移动的正确方式是通过形状的位置减去区域的位置并将结果归一化来找到的
                    //我们必须使用transform.position而不是本地位置，因为生成区域不需要是根对象。
                    direction = (t.localPosition - transform.position).normalized;
                    break;
                case SpawnConfiguration.MoveMentDirection.Random:
                    direction = Random.onUnitSphere;
                    break;
                default:
                    direction = transform.forward;
                    break;
            }

            //将行为组件天骄到形状中并设置属性
            var moveMent = shape.AddBehavior<movementShapebehavior>();
            moveMent.Velocity = direction * speed;//设置随机速度
        }
        
        
        return shape;
    }

    
    
    [System.Serializable]
    public struct SpawnConfiguration
    {
        public enum MoveMentDirection
        {//移动方向枚举
            Forward,//向前
            Upward,//向上
            Outward,//向外
            Random//随机
        }

        public ShapeFactory[] factories;

        [SerializeField]
        public MoveMentDirection moveMentDirection;
        [SerializeField]
        public FloatRange Speed;

        public FloatRange angularSpeed;//角速度
        public FloatRange scale;//比例
        public ColorRangeHSV color;//颜色

        public bool uniformColor;//颜色是否统一

    }

    

    [SerializeField]
    SpawnConfiguration spawnConfig;

}

public enum ShapeBehaviorType
    {
        Movement,
        Rotation
    }